package com.woniuxy.book.config;

import com.woniuxy.book.interceptor.MyInterceptor;
import com.woniuxy.book.interceptor.PermissionInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Resource
    private MyInterceptor myInterceptor;

    @Resource
    private PermissionInterceptor permissionInterceptor;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有接口
                .allowCredentials(true) // 是否发送 Cookie
                .allowedOrigins("*") // 支持域
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
                .allowedHeaders("*")
                .exposedHeaders("*");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor).
                excludePathPatterns("/account/login","/bookType/admin/selectBookTypeTree","/bookInfo/admin/searchBookInfos","/mq/**").//白名单
                addPathPatterns("/**").//黑名单
                excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**").
                excludePathPatterns("/userInfo/sendRegisterCode","/userInfo/sendLoginCode","/userInfo/login","/userInfo/register").
                excludePathPatterns("/userInfo/user/selectCollectRank","/userInfo/user/selectCollectRankList","/bookInfo/user/findBookInfoById","/bookInfo/user/selectBookInfos").
                excludePathPatterns("/orderInfo/notify");//白名单

        registry.addInterceptor(permissionInterceptor).
                excludePathPatterns("/account/login","/bookType/admin/selectBookTypeTree","/bookInfo/admin/searchBookInfos","/mq/**").//白名单
                addPathPatterns("/**").//黑名单
                excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**").
                excludePathPatterns("/userInfo/sendRegisterCode","/userInfo/sendLoginCode","/userInfo/login","/userInfo/register").
                excludePathPatterns("/userInfo/user/selectCollectRank","/userInfo/user/selectCollectRankList","/bookInfo/user/findBookInfoById","/bookInfo/user/selectBookInfos").
                excludePathPatterns("/orderInfo/notify");//白名单
    }

}